package cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.sql;

import cn.iocoder.yudao.framework.common.genCode.common.StringBuilderHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenColumnDO;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenTableDO;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.GlobalBinding;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.LocalBinding;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.PathHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.CodeGenTemplate;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class Sql implements CodeGenTemplate {

    public String getPath(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        return "sql/" + table.getTableName() + "/sql.sql";
    }

    public String render(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        StringBuilderHandler code = new StringBuilderHandler();
        code.apAndBl("-- 菜单 SQL");
        code.apAndBl("INSERT INTO system_menu(");
        code.apAndBl("    name, permission, menu_type, sort, parent_id,");
        code.apAndBl("    path, icon, component, status");
        code.apAndBl(")");
        code.apAndBl("VALUES (");
        code.apAndBl("    '" + table.getClassComment() + "管理', '', 2, 0, " + table.getParentMenuId() + ",");
        code.apAndBl("    '" + localBinding.getSimpleClassName_strikeCase() + "', '', '" + table.getModuleName() + "/" + localBinding.getClassNameVar() + "/index', 0");
        code.apAndBl(");");
        code.bl();
        code.apAndBl("-- 按钮父菜单ID");
        code.apAndBl("SELECT @parentId := LAST_INSERT_ID();");
        code.bl();
        code.apAndBl("-- 按钮 SQL");
        List<String> functionNames = getFunctionNames();
        for (int i = 0; i < functionNames.size(); i++) {
            String functionName = functionNames.get(i);
            code.apAndBl("INSERT INTO system_menu(");
            code.apAndBl("    name, permission, menu_type, sort, parent_id,");
            code.apAndBl("    path, icon, component, status");
            code.apAndBl(")");
            code.apAndBl("VALUES (");
            code.apAndBl("    '" + table.getClassComment() + functionName + "', '" + localBinding.getPermissionPrefix() + ":" + getFunctionOps().get(i).trim() + "', 3, " + (i + 1) + ", @parentId,");
            code.apAndBl("    '', '', '', 0");
            code.apAndBl(");");
        }
        code.bl();
        return code.toString();
    }

    private List<String> getFunctionNames() {
        String s = "查询, 创建, 更新, 删除, 导出";
        return Arrays.asList(s.split(","));
    }

    private List<String> getFunctionOps() {
        String s = "query, create, update, delete, export";
        return Arrays.asList(s.split(","));
    }

}
